﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Timers;
using Resources;
using System.Resources;
using System.Reflection;
using System.Threading;
using MyUtils;

namespace CoffeeManager
{
    public partial class Main : DevExpress.XtraEditors.XtraForm
    {
      
        System.Timers.Timer tmLoading=new System.Timers.Timer();
        LANGUAGE currentLanguage = ResourceHelper.GetCurrentLanguage();
        DevExpress.Utils.WaitDialogForm wdialog;
        String titleLoading = String.Empty;
        String captionLoading = String.Empty;
        String contentCloseDialog = String.Empty;
     
        public Main()
        {
            InitializeComponent();
            ApplyLanguage(currentLanguage);
            ApplyImage();
        }
        /// <summary>
        /// Main Load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        #region Main load
        private void Main_Load(object sender, EventArgs e)
        {
            //Load Event
            LoadEvent();
           
            //Setting main window 
            this.Icon = ResourceHelper.LoadIcon(ResourceIDs.IMG_LOGO_ICON);
            this.WindowState = FormWindowState.Minimized;
            this.ShowInTaskbar = false;
            this.Width = Screen.PrimaryScreen.WorkingArea.Width;
            this.Height = Screen.PrimaryScreen.WorkingArea.Height;
            //SetRobinControl
            rbcontrol.Width = Screen.PrimaryScreen.WorkingArea.Width;
            rbcontrol.SelectedPage = rbcontrol.Pages[0];//Set default page in begin is general tab Nov 9, 2011
            this.WindowState = FormWindowState.Normal; 
            this.ShowInTaskbar = true;
             
        # if !DEBUG  
            //Only show loading dialog in release vesion
              //Set timer
           int  timeDelay = (int)MyUtils.IntBase.INT_DELAY_TIME_LOADING; 
            tmLoading = new System.Timers.Timer(timeDelay);
            tmLoading.Elapsed += new ElapsedEventHandler(OnLoadingTimerEvent);
            tmLoading.Start();
            //Setting wait dialog
            ShowDialogLoading();
        #endif
            //Load NTP
            Load_NTP();

        }
        #endregion


        /// <summary>
        /// Timer loading
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        #region Loading timer
        private void OnLoadingTimerEvent(object source, ElapsedEventArgs e)
        {

                  wdialog.Close(); 
               //  stop timer loading 
                 tmLoading.Stop(); 
             //full screen main window   
                this.WindowState = FormWindowState.Normal;

        }
        #endregion 

        /// <summary>
        /// Apply for langugue
        /// </summary>
        #region Apply Language
        void ApplyLanguage(LANGUAGE lq)
        {
            
            //Title parent dialog
            this.Text = ResourceHelper.LoadString(ResourceIDs.STR_PARENT_TITLE);
            //Close Dialog
            contentCloseDialog = ResourceHelper.LoadString(ResourceIDs.STR_CONTENT_CLOSE_DIALOG);
            //Loadstring for Loading Dialog
            titleLoading = ResourceHelper.LoadString(ResourceIDs.STR_LOADING_TITLE);
            captionLoading = ResourceHelper.LoadString(ResourceIDs.STR_LOADING_CAPTION);
           //Loadstring for page
            rbPageDish.Text = ResourceHelper.LoadString( ResourceIDs.STR_ROBINPAGE_DISH);
            rbPageGeneral.Text = ResourceHelper.LoadString(ResourceIDs.STR_ROBINPAGE_GENERAL);
            rbPageStatistics.Text = ResourceHelper.LoadString( ResourceIDs.STR_ROBINPAGE_STATISTIC);
            rbPageManageTable.Text = ResourceHelper.LoadString( ResourceIDs.STR_ROBINPAGE_MANAGETABLE);
            rbPageImportExport.Text = ResourceHelper.LoadString(ResourceIDs.STR_ROBINPAGE_IMPORTEXPORT);
            //- End Load string for page
            #region Load string for control in GENERAL
            rbPageGroupGeneralConnection.Text = ResourceHelper.LoadString(ResourceIDs.STR_GB_CONNECTION);
            barbtnLogin.Caption = ResourceHelper.LoadString(ResourceIDs.STR_BTN_LOGIN);
            barbtnLogout.Caption = ResourceHelper.LoadString(ResourceIDs.STR_BTN_LOGOUT);

            rbPageGroupGeneralAccount.Text = ResourceHelper.LoadString(ResourceIDs.STR_GB_ACCOUNT);
            barbtnAddNewUser.Caption = ResourceHelper.LoadString(ResourceIDs.STR_BTN_ADDNEWUSER);
            barbtnModifyUser.Caption = ResourceHelper.LoadString(ResourceIDs.STR_BTN_MODIFYUSER);

            rbPageGroupLanguage.Text = ResourceHelper.LoadString(ResourceIDs.STR_GB_LANGUAGE);
            barbtnLEnglish.Caption = ResourceHelper.LoadString(ResourceIDs.STR_BTN_ENGLISH);
            barbtnLVietNamese.Caption = ResourceHelper.LoadString(ResourceIDs.STR_BTN_VIETNAMESE);

            rbPageGroupGeneralAbout.Text = ResourceHelper.LoadString(ResourceIDs.STR_GB_ABOUT);
            barbtnAbout.Caption = ResourceHelper.LoadString(ResourceIDs.STR_BTN_ABOUTUS);
            barbtnHelp.Caption = ResourceHelper.LoadString(ResourceIDs.STR_BTN_HELP);

            #endregion End : Load string for control in GENERAL

        }
        #endregion

        /// <summary>
        /// All function control event
        /// </summary>
        #region Processing Event
         
        void barbtnLogin_Click(Object sender,
                          EventArgs e)
        {
            Login frLogin = new Login();
            frLogin.MdiParent = this;
            frLogin.Show();
         
        }
        //
        void OnChangeRelationalButton(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DevExpress.XtraBars.BarButtonItem button = (DevExpress.XtraBars.BarButtonItem)e.Item;
            switch (e.Item.Name)
            {
                case MyUtils.ConstString.BTN_LENGLISH:
                    {

                        if (currentLanguage.Equals(LANGUAGE.ENGLISH))
                        {
                           // MessageBox.Show("Ngon ngu hien tai la tieng Anh roi");
                     //   MessageBox.Show("lskjdf","lskjf",MessageBo
                           
                        }
                        else
                        {
                            currentLanguage = LANGUAGE.ENGLISH;
                            ResourceHelper.SaveLanguague(currentLanguage);
                            ApplyLanguage(LANGUAGE.ENGLISH);
                            
                        }
                       
                        break;
                    }


                case MyUtils.ConstString.BTN_LVIETNAMESE:
                    {
                        if (currentLanguage.Equals(LANGUAGE.VIETNAMESE))
                        {
                            MessageBox.Show("Ngon ngu hien tai la tieng Viet roi");

                        }
                        else
                        {
                            currentLanguage =  LANGUAGE.VIETNAMESE;
                            ResourceHelper.SaveLanguague(currentLanguage);
                            ApplyLanguage(LANGUAGE.VIETNAMESE);

                        }
                       
                    }
                    break;

            }
                
                 
        }

        /// <summary>
        /// button edit diagram
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void barbtnManagerDiagramTable_Click(Object sender, EventArgs e) 
        {
            EditDiagramTables editdgram = new EditDiagramTables();
         
            editdgram.Location = new Point(20, 20);
            editdgram.MdiParent = this;
            editdgram.Show();
            
        }
        void barbtnAbout_Click(object sender, EventArgs e)
        {
            FrogAbout frogAbout = new FrogAbout();
            frogAbout.ShowDialog();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Main_FormClosing(Object sender,  System.ComponentModel.CancelEventArgs  e)
        {
            MyMessageBoxResults result = MyMessageBox.Show(contentCloseDialog, this.Text, MyMessageBoxButtons.YesNo, MyMessageBoxIcons.Question);
          if (result == MyMessageBoxResults.No)
              e.Cancel = true;
        }
        #endregion


        /// <summary>
        /// Apply Image for image control
        /// </summary>
        #region  Apply Bitmap Resources
        void ApplyImage()
        {
            #region General Page
            barbtnLogin.Glyph = ResourceHelper.LoadBitmap(ResourceIDs.IMG_BTN_LOGIN);
            barbtnLogout.Glyph = ResourceHelper.LoadBitmap(ResourceIDs.IMG_BTN_LOGOUT);
            barbtnAddNewUser.Glyph = ResourceHelper.LoadBitmap(ResourceIDs.IMG_BTN_ADDUSER);
            barbtnModifyUser.Glyph = ResourceHelper.LoadBitmap(ResourceIDs.IMG_BTN_EDITUSER);
            barbtnLEnglish.Glyph = ResourceHelper.LoadBitmap(ResourceIDs.IMG_BTN_LENGLISH);
            barbtnLVietNamese.Glyph = ResourceHelper.LoadBitmap(ResourceIDs.IMG_BTN_LVIETNAMESE);
            barbtnAbout.Glyph = ResourceHelper.LoadBitmap(ResourceIDs.IMG_BTN_ABOUTUS);
            barbtnHelp.Glyph = ResourceHelper.LoadBitmap(ResourceIDs.IMG_BTN_HELP);
            #endregion

           
        }
        #endregion

        #region ShowDiaglogLoading
        void ShowDialogLoading()
        {
            //Depend on languague , load

            wdialog = new DevExpress.Utils.WaitDialogForm(captionLoading, titleLoading, MyUtils.DataTypeBase.SIZE_LOADING_DIALOG);
           
        }
        #endregion

        #region Load NTPForm
        void Load_NTP()
        {
            MyUtils.ModifyRegistry registry = new ModifyRegistry();
            object value = registry.Read(MyUtils.ConstString.REGEDIT_CHECK_NTP);
            if (value == null)
            {
                NTPForm ntpForm = new NTPForm(); 
                ntpForm.ShowDialog();
            }
        }
        #endregion
    }
}
